package org.itboys.core.controller.web.tray;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.common.UserTypeEnum;
import org.itboys.core.dto.tray.SundayTrayApplyDto;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.entity.mongo.SundayTrayApply;
import org.itboys.core.entity.mysql.tray.SundayTrayModel;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.core.service.mongo.web.SundayTrayApplyService;
import org.itboys.core.service.mysql.web.SundayTrayModelService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/12/5
 * 描述：托盘投放
 */
@Controller
@RequestMapping("/sunday/web/apply")
@Api(value = "apply", description = "托盘投放")
public class SundayTrayApplyController extends BaseController {
    @Autowired
    private SundayTrayApplyService applyService;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayTrayModelService modelService;


    /**
     * 主页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model
    ) {
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("userType", UserTypeEnum.SUPPLIER.getCode());
        List<SundayMember> members = memberService.list(param);
        model.addAttribute("companys", members);
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        List<SundayTrayModel> models = modelService.select(param);
        model.addAttribute("models", models);
        return "/sunday/tray/apply/index";
    }

    /**
     * 分页查询
     *
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        PageResult<SundayTrayApplyDto> result = applyService.select(request);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }

    /**
     * @param request
     * @param response
     * @param applyId
     * @param status
     */
    @RequestMapping("/handle")
    public void handle(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "applyId", required = true) Long applyId,
            @RequestParam(value = "status", required = true) Integer status,
            @RequestParam(value = "startId", required = false) Long startId,
            @RequestParam(value = "endId", required = false) Long endId
    ) {
        applyService.handle(applyId, status, startId, endId);
        ResponseMessage.success();
    }


}
